<template>
  <view class="apps-container">
    <!-- 顶部导航栏 -->
    <view class="nav-bar">
      <view class="nav-left" @click="goBack">
        <uni-icons type="back" size="24"></uni-icons>
      </view>
      <view class="nav-title">应用中心</view>
      <view class="nav-right" @click="toggleEdit">
        <text>{{isEditing ? '完成' : '编辑'}}</text>
      </view>
    </view>
    
    <!-- 应用分类区域 -->
    <view class="apps-content">
      <!-- 首页应用 -->
      <view class="app-section">
        <view class="section-header">
          <text class="section-title">首页应用</text>
          <!-- 移除了首页常用应用的描述文字 -->
        </view>
        <view class="app-grid">
          <view class="app-item" v-for="(item, index) in homeApps" :key="index">
            <view class="app-icon-wrapper">
              <image class="app-icon" :src="item.icon" mode="aspectFit"></image>
              <view class="delete-badge" v-if="isEditing" @click.stop="removeApp(item, 'home')">
                <uni-icons type="close" size="12" color="#fff"></uni-icons>
              </view>
            </view>
            <text class="app-name">{{item.name}}</text>
          </view>
        </view>
      </view>
      
      <!-- 更多应用 -->
      <view class="app-section">
        <view class="section-header">
          <text class="section-title">更多应用</text>
          <!-- 移除了更多功能应用的描述文字 -->
        </view>
        <view class="app-grid">
          <view class="app-item" v-for="(item, index) in moreApps" :key="index" @click="openApp(item)">
            <view class="app-icon-wrapper">
              <image class="app-icon" :src="item.icon" mode="aspectFit"></image>
              <view class="new-badge" v-if="item.isNew">新技能</view>
            </view>
            <text class="app-name">{{item.name}}</text>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      isEditing: false,
      homeApps: [
        { name: '房源', icon: '/static/tabbar/svg/house.svg', url: '/pages/house/index' },
        { name: '合同', icon: '/static/tabbar/svg/contract.svg', url: '/pages/contract/index' },
        { name: '消息', icon: '/static/tabbar/svg/message.svg', url: '/pages/message/index' }
      ],
      moreApps: [
        { name: '硬件商城', icon: '/static/apps/hardware.svg', url: '', isNew: false },
        { name: '达摩院', icon: '/static/apps/academy.svg', url: '', isNew: false },
        { name: 'AI小二', icon: '/static/apps/ai.svg', url: '', isNew: true },
        { name: '运营配置', icon: '/static/apps/operation.svg', url: '/pages/operation/index', isNew: false }
      ]
    }
  },
  methods: {
    goBack() {
      uni.navigateBack();
    },
    toggleEdit() {
      this.isEditing = !this.isEditing;
    },
    removeApp(app, type) {
      if (type === 'home') {
        const index = this.homeApps.findIndex(item => item.name === app.name);
        if (index !== -1) {
          this.homeApps.splice(index, 1);
        }
      }
    },
    openApp(app) {
      if (app.url) {
        uni.navigateTo({
          url: app.url
        });
      } else {
        uni.showToast({
          title: '功能开发中',
          icon: 'none'
        });
      }
    }
  }
}
</script>

<style lang="scss">
.apps-container {
  min-height: 100vh;
  background-color: #f5f5f5;
  padding-bottom: 30rpx;
}

.nav-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20rpx;
  background-color: #fff;
  position: sticky;
  top: 0;
  z-index: 100;
  
  .nav-left {
    width: 60rpx;
  }
  
  .nav-title {
    font-size: 36rpx;
    font-weight: bold;
  }
  
  .nav-right {
    width: 60rpx;
    text-align: right;
    font-size: 28rpx;
    color: #1890ff;
  }
}

.apps-content {
  padding: 20rpx;
}

.app-section {
  background-color: #fff;
  border-radius: 16rpx;
  padding: 30rpx;
  margin-bottom: 30rpx;
}

.section-header {
  margin-bottom: 30rpx;
  
  .section-title {
    font-size: 32rpx;
    font-weight: bold;
    color: #333;
    margin-bottom: 10rpx;
    display: block;
  }
  
  .section-desc {
    font-size: 24rpx;
    color: #999;
  }
}

.app-grid {
  display: flex;
  flex-wrap: wrap;
}

.app-item {
  width: 25%;
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-bottom: 30rpx;
}

.app-icon-wrapper {
  position: relative;
  margin-bottom: 16rpx;
}

.app-icon {
  width: 100rpx;
  height: 100rpx;
  border-radius: 20rpx;
  background-color: #f0f5ff;
}

.app-name {
  font-size: 24rpx;
  color: #333;
}

.new-badge {
  position: absolute;
  top: -10rpx;
  right: -10rpx;
  background-color: #ff4d4f;
  color: #fff;
  font-size: 20rpx;
  padding: 4rpx 10rpx;
  border-radius: 20rpx;
}

.delete-badge {
  position: absolute;
  top: -10rpx;
  right: -10rpx;
  background-color: #ff4d4f;
  color: #fff;
  width: 36rpx;
  height: 36rpx;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}
</style>